﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="UnspscCodeTests.cs" company="wwwlicious">
//   All rights reserved 2009.
// </copyright>
// <summary>
//   Summary description for UnspscCodeTests
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Unspsc.Tests
{
    #region

    using System;

    using NUnit.Framework;

    #endregion

    /// <summary>
    /// Summary description for UnspscCodeTests
    /// </summary>
    [TestFixture]
    public class UnspscCodeTests
    {
        [Test]
        public void UnspscCodeCorrectCodePopulatesFields()
        {
            UnspscCode code = 12345678;

            Assert.AreEqual(12000000, code.Segment);
            Assert.AreEqual(false, code.IsSegment);

            Assert.AreEqual(12340000, code.Family);
            Assert.AreEqual(false, code.IsFamily);

            Assert.AreEqual(12345600, code.Class);
            Assert.AreEqual(false, code.IsClass);

            Assert.AreEqual(12345678, code.Commodity);
            Assert.AreEqual(true, code.IsCommodity);
        }

        [Test]
        public void UnspscCodeIncorrectMinCodeThrowsException()
        {
            Assert.Throws<ArgumentException>(() => { UnspscCode code = UnspscCode.MinValue - 1; });
        }

        [Test]
        public void UnspscCodeIncorrectMaxCodeThrowsException()
        {
            Assert.Throws<ArgumentException>(() => { UnspscCode code = UnspscCode.MaxValue + 1; });
        }

        [Test]
        public void UnspscCodeMinValueCanBeInitialised()
        {
            UnspscCode code = UnspscCode.MinValue;
            Assert.AreEqual(UnspscCode.MinValue, (double)code);
        }

        [Test]
        public void UnspscCodeMaxValueCanBeInitialised()
        {
            UnspscCode code = UnspscCode.MaxValue;
            Assert.AreEqual(UnspscCode.MaxValue, (double)code);
        }

        [Test]
        public void UnspscCodeEqualsOperatorMatchesIdenticalCodes()
        {
            UnspscCode code = UnspscCode.MinValue;
            UnspscCode code2 = UnspscCode.MinValue;
            Assert.IsTrue(code == code2);
        }

        [Test]
        public void UnspscCodeEqualsOperatorFailsDifferentCodes()
        {
            UnspscCode code = UnspscCode.MinValue;
            UnspscCode code2 = UnspscCode.MaxValue;
            Assert.IsFalse(code == code2);
        }

        [Test]
        public void UnspscCodeNotEqualsOperatorFailsWithIdenticalCodes()
        {
            UnspscCode code = UnspscCode.MinValue;
            UnspscCode code2 = UnspscCode.MinValue;
            Assert.IsFalse(code != code2);
        }

        [Test]
        public void UnspscCodeNotEqualsOperatorPassesDifferentCodes()
        {
            UnspscCode code = UnspscCode.MinValue;
            UnspscCode code2 = UnspscCode.MaxValue;
            Assert.IsTrue(code != code2);
        }

        [Test]
        public void UnspscCodeGetTypeCodeTest()
        {
            UnspscCode target = new UnspscCode(); 
            Object expected = TypeCode.Double; 
            TypeCode actual = target.GetTypeCode();
            Assert.AreEqual(expected, actual);
        }

        [Test]
        public void UnspscCodeCompareToTest()
        {
            UnspscCode target = UnspscCode.MinValue;
            UnspscCode other = UnspscCode.MinValue;
            int expected = 0;
            int actual = target.CompareTo(other);
            Assert.AreEqual(expected, actual);
        }

        [Test]
        public void UnspscCodeObjectEqualsTest()
        {
            UnspscCode target = UnspscCode.MinValue;
            UnspscCode obj = UnspscCode.MinValue;
            bool expected = true;
            bool actual;
            actual = target.Equals(obj);
            Assert.AreEqual(expected, actual);
        }

        [Test]
        public void UnspscCodeEqualsTest()
        {
            UnspscCode target = UnspscCode.MinValue;
            UnspscCode other = UnspscCode.MinValue;
            bool expected = true;
            bool actual = target.Equals(other);
            Assert.AreEqual(expected, actual);
        }

        [Test]
        public void UnspscCodeGetHashCodeTest()
        {
            UnspscCode target = UnspscCode.MinValue;
            int expected = 1097011920;
            int actual = target.GetHashCode();
            Assert.AreEqual(expected, actual);
        }

        [Test]
        public void UnspscCodeImplicitConversionToDecimal()
        {
            UnspscCode value = UnspscCode.MinValue;
            decimal actual = value;
            decimal expected = 10000000;
            Assert.AreEqual(expected, actual);
        }

        [Test]
        public void UnspscCodeImplicitConversionToLong()
        {
            UnspscCode value = UnspscCode.MinValue;
            long actual = value;
            long expected = 10000000;
            Assert.AreEqual(expected, actual);
        }

        [Test]
        public void UnspscCodeImplicitConversionToIntThrowsException()
        {
            int number = 1;
            Assert.Throws<ArgumentException>(() => { UnspscCode code = (int)number; });
        }

        [Test]
        public void UnspscCodeImplicitConversionToShortThrowsException()
        {
            short number = 1;
            Assert.Throws<ArgumentException>(() => { UnspscCode code = (int)number; });
        }

        #region Explicit conversion tests

        [Test]
        public void UnspscCodeExplicitConversionToBooleanThrowsException()
        {
            UnspscCode code = UnspscCode.MinValue;
            Assert.Throws<NotSupportedException>(() => code.ToBoolean(null));
        }

        [Test]
        public void UnspscCodeExplicitConversionToByteThrowsException()
        {
            UnspscCode code = UnspscCode.MinValue;
            Assert.Throws<NotSupportedException>(() => code.ToByte(null));
        }

        [Test]
        public void UnspscCodeExplicitConversionToCharThrowsException()
        {
            UnspscCode code = UnspscCode.MinValue;
            Assert.Throws<NotSupportedException>(() => code.ToChar(null));
        }

        [Test]
        public void UnspscCodeExplicitConversionToDateTimeThrowsException()
        {
            UnspscCode code = UnspscCode.MinValue;
            Assert.Throws<NotSupportedException>(() => code.ToDateTime(null));
        }

        [Test]
        public void UnspscCodeExplicitConversionToInt16ThrowsException()
        {
            UnspscCode code = UnspscCode.MinValue;
            Assert.Throws<NotSupportedException>(() => code.ToInt16(null));
        }

        [Test]
        public void UnspscCodeExplicitConversionToInt32ThrowsException()
        {
            UnspscCode code = UnspscCode.MinValue;
            Assert.Throws<NotSupportedException>(() => code.ToInt32(null));
        }

        [Test]
        public void UnspscCodeExplicitConversionToSByteThrowsException()
        {
            UnspscCode code = UnspscCode.MinValue;
            Assert.Throws<NotSupportedException>(() => code.ToSByte(null));
        }

        [Test]
        public void UnspscCodeExplicitConversionToSingleThrowsException()
        {
            UnspscCode code = UnspscCode.MinValue;
            Assert.Throws<NotSupportedException>(() => code.ToSingle(null));
        }

        [Test]
        public void UnspscCodeExplicitConversionToUInt16ThrowsException()
        {
            UnspscCode code = UnspscCode.MinValue;
            Assert.Throws<NotSupportedException>(() => code.ToUInt16(null));
        }

        [Test]
        public void UnspscCodeExplicitConversionToUInt32ThrowsException()
        {
            UnspscCode code = UnspscCode.MinValue;
            Assert.Throws<NotSupportedException>(() => code.ToUInt32(null));
        }

        [Test]
        public void UnspscCodeExplicitConversionToDecimal()
        {
            UnspscCode code = UnspscCode.MinValue;
            Assert.AreEqual((decimal)code, code.ToDecimal(null));
        }

        [Test]
        public void UnspscCodeToString()
        {
            UnspscCode code = UnspscCode.MinValue;
            string test = code.ToString();
            Assert.True(test == "10-00-00-00");
        }

        #endregion
    }
}